package com.sale.system.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sale.common.core.utils.StringUtils;
import com.sale.system.api.RemoteInventoryInquiryService;
import com.sale.system.api.domain.dto.SysAuxiliaryUnitDateDetailResultDto;
import com.sale.system.api.domain.dto.SysMaterialFinishedResultDto;
import com.sale.system.api.domain.dto.SysMaterialRawResultDto;
import com.sale.system.api.model.MaterialUnitDto;
import com.sale.system.domain.SysAuxiliaryData;
import com.sale.system.domain.SysAuxiliaryUnit;
import com.sale.system.domain.SysAuxiliaryUnitDetail;
import com.sale.system.domain.dto.MaterialFinishedDto;
import com.sale.system.domain.dto.MaterialRawDto;
import com.sale.system.domain.dto.SysAuxiliaryUnitDateDetailDto;
import com.sale.system.mapper.SysAuxiliaryDataMapper;
import com.sale.system.mapper.SysAuxiliaryUnitDetailMapper;
import com.sale.system.mapper.SysAuxiliaryUnitMapper;
import com.sale.system.service.IMaterialFinishedService;
import com.sale.system.service.IMaterialRawService;
import com.sale.system.service.ISysAuxiliaryDataService;
import com.sale.system.service.ISysAuxiliaryUnitService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: YSL
 * @Date: 2023/7/27 15:04
 * @Desc: 库存信息 dubbo
 */
@RequiredArgsConstructor
@Service
@DubboService
public class RemoteInventoryInquiryServiceImpl implements RemoteInventoryInquiryService {

    /**
     * 原料信息
     */
    private final IMaterialRawService iMaterialRawService;

    /**
     * 成品信息
     */
    private final IMaterialFinishedService iMaterialFinishedService;

    /**
     * 辅助资料（单计量单位查询）
     */
    private final ISysAuxiliaryDataService iSysAuxiliaryDataService;

    /**
     * 多计量单位
     */
    private final ISysAuxiliaryUnitService iSysAuxiliaryUnitService;

    private final SysAuxiliaryUnitMapper sysAuxiliaryUnitMapper;

    private final SysAuxiliaryUnitDetailMapper sysAuxiliaryUnitDetailMapper;

    private final SysAuxiliaryDataMapper sysAuxiliaryDataMapper;

    /**
     * 原料信息
     *
     * @param materialCode
     * @return
     */
    @Override
    public SysMaterialRawResultDto getByMaterialCode(String materialCode) throws Exception {
        SysMaterialRawResultDto sysMaterialRawResultDto = new SysMaterialRawResultDto();
        MaterialRawDto byMaterialCode = iMaterialRawService.getByMaterialCode(materialCode);
        BeanUtils.copyProperties(byMaterialCode, sysMaterialRawResultDto);
        return sysMaterialRawResultDto;
    }

    /**
     * 成品信息
     *
     * @param materialCode
     * @return
     */
    @Override
    public SysMaterialFinishedResultDto getByFinishedMaterialCode(String materialCode) throws Exception {
        SysMaterialFinishedResultDto sysMaterialFinishedResultDto = new SysMaterialFinishedResultDto();
        MaterialFinishedDto materialFinishedDto = iMaterialFinishedService.getByFinishedMaterialCode(materialCode);
        BeanUtils.copyProperties(materialFinishedDto, sysMaterialFinishedResultDto);
        return sysMaterialFinishedResultDto;
    }

    /**
     * 获取单计量单位信息
     *
     * @param auxiliaryId
     * @return
     */
    @Override
    public MaterialUnitDto getByAuxiliaryData(Long auxiliaryId) throws Exception {
        MaterialUnitDto materialUnitDto = new MaterialUnitDto();
        SysAuxiliaryData byAuxiliaryData = iSysAuxiliaryDataService.getByAuxiliaryData(auxiliaryId);
        if (byAuxiliaryData == null) {
            throw new Exception("单计量单位为空");
        }
        BeanUtils.copyProperties(byAuxiliaryData, materialUnitDto);
        materialUnitDto.setUnitName(byAuxiliaryData.getAuxiliaryName());
        return materialUnitDto;
    }

    /**
     * 根据id查询多计量单位
     *
     * @param unitId
     * @return
     */
    @Override
    public SysAuxiliaryUnitDateDetailResultDto getMaterialUnitById(Long unitId, Long materialId) throws Exception {
        SysAuxiliaryUnitDateDetailResultDto sysAuxiliaryUnitDateDetailResultDto = new SysAuxiliaryUnitDateDetailResultDto();
//        //查单单位
//        SysAuxiliaryData sysAuxiliaryData = sysAuxiliaryDataMapper.selectOne(new QueryWrapper<SysAuxiliaryData>()
//            .lambda().eq(SysAuxiliaryData::getAuxiliaryId, unitId));
//        if(sysAuxiliaryData != null) {
//            //查单个单位
//            sysAuxiliaryUnitDateDetailResultDto.setUnitName(sysAuxiliaryData.getAuxiliaryName());
//        }else {
//            //查多个单位的基础单位
//            SysAuxiliaryUnitDateDetailResultDto sysAuxiliaryUnitDateDetailResultDto1 = sysAuxiliaryUnitDetailMapper.selectMoreUnitBasics(materialId);
//            sysAuxiliaryUnitDateDetailResultDto.setMoreUnitName(sysAuxiliaryUnitDateDetailResultDto1.getMoreUnitName());
////            materialUnitById = iSysAuxiliaryUnitService.getMaterialUnitById(unitId);
//        }
        //查多个单位的基础单位
        List<SysAuxiliaryUnitDateDetailResultDto> sysAuxiliaryUnitDateDetailResultDto1 = sysAuxiliaryUnitDetailMapper.selectMoreUnitBasics(materialId);
        StringBuilder stringBuilder = new StringBuilder();
        for (SysAuxiliaryUnitDateDetailResultDto entity : sysAuxiliaryUnitDateDetailResultDto1) {
            stringBuilder.append(entity.getMoreUnitName()).append(",");
        }
        if(StringUtils.isNotEmpty(stringBuilder)){
            StringBuilder stringBuilder1 = stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            sysAuxiliaryUnitDateDetailResultDto.setMoreUnitName(stringBuilder1.toString());
        }
//        if(materialUnitById != null){
//            BeanUtils.copyProperties(materialUnitById, sysAuxiliaryUnitDateDetailResultDto);
//            sysAuxiliaryUnitDateDetailResultDto.setAuxiliaryUnitName(materialUnitById.getAuxiliaryUnitName());
//        }
        return sysAuxiliaryUnitDateDetailResultDto;
    }

    /**
     * 多单位换算
     *
     * @param id
     * @param unitQuantity
     * @return
     */
    @Override
    public MaterialUnitDto selectMaterialUnitList(Long id, Long unitQuantity) {
        return iSysAuxiliaryUnitService.selectMaterialUnitList(id, unitQuantity);
    }

    /**
     * 查询多单位换算(采购打印明细列表使用)
     *
     * @return
     */
    @Override
    public MaterialUnitDto selectAtWillMaterialUnitList(Long id, Long unitQuantity) {
        return iSysAuxiliaryUnitService.selectAtWillMaterialUnitList(id, unitQuantity);
    }


}
